var $UTYPE="0",$POSMENU=0;
CHECKLOGIN=function(){}
CHECKLOGIN.prototype.check = function(){
	var fncSuc = XHR.Delegate(this,this.checkSuc);
	var param = {act:"checklogin"};
	new XHR("post",fncSuc).send($PN+"/DoLogin",param);
}
CHECKLOGIN.prototype.checkSuc=function(r){
	var xml = r.xml;
	if(getRV(xml)){
		var uid = xml.getElementsByTagName("root")[0].getAttribute("uid");
		$UTYPE = xml.getElementsByTagName("root")[0].getAttribute("utype");
		genMenu($POSMENU);
		$("username").innerHTML = "<span style='font-weight:bold;color:white'>Welcome "+uid+"</span> | " +
				"<span style='cursor:pointer;color:white' onClick='showChangePassword()' id='changePassBtn'>[Change password]</span> ";
	}else{
		window.location="/TopNewsManager";
		return;
	}
}
function showChangePassword(){
	var pos = findPos($("changePassBtn"));
	setPos("changepass_div",pos[1],pos[0]-150);
	$("changepass_div").style.display="block";
}
UPDATEPASS=function(){}
UPDATEPASS.prototype.update=function(oldpass,newpass){
	var fncSuc = XHR.Delegate(this,this.updateSuc);
	var param = {oldpass:oldpass,password:newpass};
	new XHR("post",fncSuc).send($PN+"/DoChangePassword",param);
}
UPDATEPASS.prototype.updateSuc=function(r){
	var xml = r.xml;
	if(getRV(xml)){
		alert("Cập nhật thành công");
		$('changepass_div').style.display='none'
	}else{
		window.location="/NewsManager";
		return;
	}
}
function updatePassword(){
	var oldpass = $N("old_password")[0].value;
	var newpass = $N("password")[0].value;
	var c_newpass = $N("c_password")[0].value;
	if(oldpass=="") return;

	if(newpass!=c_newpass || newpass.length <5){
		alert("Mật khẩu không hợp lệ. Tối thiểu 5 ký tự");
		return;
	}
	new UPDATEPASS().update(oldpass,newpass);
}
var $MENU=["Tin tức:news.html","Tin chính:order.html","Video Clip:clip.html"];
var $MENUADMIN=["Tin tức:news.html","Tin chính:order.html","Video Clip:clip.html", "Tài khoản:user.html"];
function genMenu(pos){
	var html = "",cl="menu_nc",cl_font="white";
	var menu = $MENU;
	if($UTYPE==1)
		menu = $MENUADMIN;
	for(var i=0; i < menu.length; i++){
		if(i==pos){cl="menu_c";cl_font="red";}
		else {cl = "menu_nc";cl_font="white";}
		html+='<div class="'+cl+'">'+
				'<a style="color:'+cl_font+';text-decoration:none" href="'+menu[i].split(":")[1]+'">'+menu[i].split(":")[0]+'</a>'+
			  '</div>'+
	          '<div style="float:left;"><img src="images/line.jpg" width="2" height="35" /></div>';
	}	
	$("menu").innerHTML = html;
}